//
//  AddCustomBiaoQianViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/25.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//***************添加自定义标签***************
#import "AddCustomBiaoQianViewController.h"

@interface AddCustomBiaoQianViewController ()
@property(nonatomic,copy)UITextField *myTextField;
@property(nonatomic,copy)NSTimer *myTimer;
@property(nonatomic,assign)NSInteger myTime;
@property(nonatomic,copy)UIButton *changeBtn;
@end

@implementation AddCustomBiaoQianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self createNavigation];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)createNavigation
{
    self.navigationController.navigationBar.hidden=NO;
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"添加自定义标签";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
    
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createUI
{
    UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(10, 20+44+20, self.view.frame.size.width-10*2, 40)];
    textField.placeholder=@"请填写标签";
    textField.font=[UIFont systemFontOfSize:15];
    textField.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    textField.backgroundColor=[UIColor whiteColor];
    textField.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    textField.layer.borderWidth=2.0;
    textField.layer.cornerRadius=5.0;
    [self.view addSubview:textField];
    //提示
    UILabel *promptMessageLab=[[UILabel alloc]initWithFrame:CGRectMake(textField.frame.origin.x, CGRectGetMaxY(textField.frame)+2, textField.frame.size.width, 15)];
    promptMessageLab.text=@"标签的长度为5字以内,可以使用英文或汉字等";
    promptMessageLab.textColor=[UIColor grayColor];
    promptMessageLab.numberOfLines=0;
    promptMessageLab.font=[UIFont systemFontOfSize:10];
    [self.view addSubview:promptMessageLab];
    
    //确认修改
    UIButton *changeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    changeBtn.frame=CGRectMake((self.view.frame.size.width-150)/2.0, CGRectGetMaxY(promptMessageLab.frame)+15, 150, 35);
    [changeBtn setTitle:@"确认" forState:UIControlStateNormal];
    [changeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    changeBtn.titleLabel.font=[UIFont systemFontOfSize:20];
    changeBtn.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    changeBtn.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    changeBtn.layer.borderWidth=2.0;
    changeBtn.layer.cornerRadius=5.0;
    [changeBtn addTarget:self action:@selector(changeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeBtn];
    
    _myTextField=textField;
    _changeBtn=changeBtn;
}

//确认修改
-(void)changeBtnClick:(UIButton *)sender
{
    if (_myTextField.text.length>0&&_myTextField.text.length<6) {
        NSLog(@"修改成功");
        [self addCustomBiaoQian];
    }
    if (_myTextField.text.length==0) {
        _changeBtn.userInteractionEnabled=NO;
        [self.view showPromptMessage:YES and:@"标签不能为空"];
        _myTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
    }
    if (_myTextField.text.length>=6) {
        _changeBtn.userInteractionEnabled=NO;
        [self.view showPromptMessage:YES and:@"标签不能超过5个字"];
        _myTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
    }
    
}
//启动定时器
-(void)timerRun
{
    _myTime++;
    if (_myTime>2) {
        [self.view showPromptMessage:NO and:nil];
        [_myTimer invalidate];
        _myTimer=nil;
        _myTime=0;
        _changeBtn.userInteractionEnabled=YES;
    }else{
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//添加自定义标签
-(void)addCustomBiaoQian
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [self.view isLoading:YES];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token= [user objectForKey:@"token"];
    
    NSDictionary *params=@{@"name":_myTextField.text,@"discription":@"hhhhhhhhhh"};
    
    [manager POST:[NSString stringWithFormat:AddCustomBiaoQian,token] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"添加自定义标签%@",dic);
        self.changeData(dic);
        [self addToMyBiaoQian:dic];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view isLoading:NO];
        NSLog(@"error%@",error.localizedDescription);
    }];
}

-(void)addToMyBiaoQian:(NSDictionary *)dataDic
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token= [user objectForKey:@"token"];
    
    [manager POST:[NSString stringWithFormat:AddToMy,token,dataDic[@"id"]] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.view isLoading:NO];
        [self.view showTiShiMessage:@"添加成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view isLoading:NO];
         NSLog(@"error%@",error.localizedDescription);
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
